<template>
  <!-- 取消随访 -->
  <el-dialog title="取消随访" :visible="value" width="400px" custom-class="dialog-box" @close="onClose" @open="onOpen" :close-on-click-modal="false" :append-to-body="true">
    <el-form ref="form" label-width="100px" :model="form" :rules="rules">
      <el-form-item label="取消原因：" prop="string">
        <el-input v-model="form.string" type="textarea" :rows="3"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer">
      <el-button type="primary" :loading="btnLoading" @click="onSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
export default {
  props: ["value", "curItem"],
  data() {
    return {
      btnLoading: false,
      form: {
        id: "",
        string: ""
      },
      rules: {
        string: [{ required: true, message: "此项必填", trigger: "blur" }]
      }
    };
  },
  methods: {
    onSubmit() {
      const th = this;
      let j = {};
      const fn = function() {
        Object.assign(j, th.form);
        th.btnLoading = true;
        th.$api
          .followUpVisitcancel(j)
          .then(res => {
            if (res.executed) {
              th.$message.success("取消成功");
              th.$emit("success");
              th.onClose();
            } else {
              th.$message.error(res.message);
            }
            th.btnLoading = false;
          })
          .catch(res => {
            th.btnLoading = false;
          });
      };
      th.$refs["form"].validate(v => {
        if (v) {
          Object.assign(j, th.form);
          fn();
        }
      });
    },
    onOpen() {
      const th = this;
      th.form.id = th.curItem.id;
    },
    onClose() {
      const th = this;
      th.$refs["form"].resetFields();
      th.$emit("input", false);
    }
  }
};
</script>